<template>
  <div>
    <Table
      ref="dataTable"
      stripe
      :loading="table.loading"
      :columns="table.tableColumns"
      :data="table.tableRows"
      style="margin-top:20px;"
      @on-select="select" @on-select-cancel="selectCancel"
               @on-select-all="selectAll" @on-select-all-cancel="selectAllCancel"
      @on-sort-change="changeSort"
    ></Table>
    <div style="margin: 20px; overflow: hidden">
      <div style="float: right;">
        <Page
          :total="pager.total"
          :current="pager.pageNo"
          @on-change="changePageNo"
          @on-page-size-change="changePageSize"
          showSizer
          showTotal
        ></Page>
      </div>
    </div>
  </div>
</template>

<script>
import * as utils from '@/api/utils-v2'
import * as ResponseStatus from '@/api/response-status'
import headImg from '@/assets/images/head.png'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
const cdnUrl = config.baseUrl.cdnUrl
export default {
  name: 'UserUserHierarchyTable',
  data() {
    return {
      urls: {
        searchUrl: '/user-userhierarchy/admin/pager-cond'
      },
      pager: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
	dataDictCodes: ['IS_ACTIVE'],
	dataDictItems: {},
      searchOpts: {
        sortColumn: null,
        sortOrder: null
      },
      table: {
        loading: false,
        tableColumns: [
          {
            type: 'selection',
            width: 52,
            key: 'id',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 60,
            align: 'center',
            fixed: 'left',
            render: (h, params) => {
              return h(
                'span',
                params.index + (this.pager.pageNo - 1) * this.pager.pageSize + 1
              )
            }
          },
          {
            title: '用户编号',
            key: 'userId',
            minWidth: 120,
            sortable: true,
            render: (h, params) => {
						  return h(
						    'Dropdown',
						    {
						      on: {
						        'on-click': itemName => {
						          this.userOpt(itemName, params.row)
						        }
						      },
						      props: {
						        transfer: true
						      }
						    },
						    [
						      h('span', [
						        params.row.userId,
						        h('Icon', {
						          props: {
						            type: 'ios-list',
						            size: '25'
						          }
						        })
						      ]),
						      h(
						        'DropdownMenu',
						        {
						          slot: 'list'
						        },
						        [
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'moduleDetail'
						              }
						            },
						            '详情'
						          ),
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'moduleSearch'
						              }
						            },
						            '搜索'
						          )
						        ]
						      )
						    ]
						  )}
},
          {
            title: '手机号',
            key: 'userPhone',
            minWidth: 120,
            sortable: true
          },
          {
            title: '用户邮箱',
            key: 'userEmail',
            minWidth: 120,
            sortable: true
          },
          {
            title: '关系编号',
            key: 'userHierarchyId',
            minWidth: 120,
            sortable: true
          },
          {
            title: '祖先编号',
            key: 'userHierarchyAncestorId',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '祖先编号'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '用户的上级编号',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '用户编号',
            key: 'userHierarchyUserId',
            minWidth: 120,
            sortable: true
          },
          {
            title: '用户级别',
            key: 'userHierarchyUserLevel',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '用户级别'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content:
                        '如1-2-3,当前用户若为3，祖先用户若为1，则level=3；自己与自己level=1',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '版本号',
            key: 'userHierarchyVersion',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '版本号'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '用户关系版本号',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '创建时间',
            key: 'userHierarchyCreateTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '创建时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '用户关系创建时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '更新时间',
            key: 'userHierarchyUpdateTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '更新时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '用户关系更新时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '是否激活',
            key: 'userHierarchyIsActive',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '是否激活'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '用户关系是否激活',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            },
			render: (h, params) => {
			        let dictItem = utils.getDataDictItemDisplayValue(this, 'IS_ACTIVE', params.row.userHierarchyIsActive)
			        return h('span', dictItem.displayValue)
			 }
          },
          {
            title: '昵称',
            key: 'userDetailNickname',
            minWidth: 120,
            sortable: true
          },
          {
            title: '头像地址',
            key: 'userDetailHeadicon',
            minWidth: 120,
            sortable: true,
            render: (h, params) => {
              let imgSrc = params.row.userDetailHeadicon
              if (!imgSrc) {
                imgSrc = headImg
              } else {
                if (imgSrc.indexOf('http') < 0) {
                  imgSrc = cdnUrl + '/' + imgSrc
                }
              }
              return h(
                'img',
                {
                  attrs: {
                    src: imgSrc
                  },
                  style: {
                    width: '60px',
                    height: '60px',
                    borderRadius: '50%'
                  }
                },
                ''
              )
            }
          },

          {
            title: '操作',
            key: 'action',
            width: 130,
            align: 'center',
            fixed: 'right',
            render: (h, params) => {
              return h(
                'Dropdown',
                {
                  on: {
                    'on-click': itemName => {
                      this.userOpt(itemName, params.row)
                    }
                  },
                  props: {
                    transfer: true
                  }
                },
                [
                  h(
                    'Button',
                    {
                      props: {
                        type: 'primary',
                        size: 'small'
                      }
                    },
                    [
                      '选择操作 ',
                      h('Icon', {
                        props: {
                          type: 'ios-arrow-down'
                        }
                      })
                    ]
                  ),
                  h(
                    'DropdownMenu',
                    {
                      slot: 'list'
                    },
                    [
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDetail'
                          }
                        },
                        '详情'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDistributionAbove'
                          }
                        },
                        '查看上级分销'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDistributionBelow'
                          }
                        },
                        '查看下级分销'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'performanceStat'
                          }
                        },
                        '业绩统计'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'putToTop'
                          }
                        },
                        '设为顶级分销商'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'putToUser'
                          }
                        },
                        '重设直接上级'
                      )
                    ]
                  )
                ]
              )
            }
          }
        ],
        tableRows: [],
        selections: [],
        toUserId: null
      }
    }
  },
  computed: {},
  mounted() {
    this.search()
  },
  methods: {
    search() {
      this.$emit('searchTable')
    },
    userOpt(itemName, row) {
      if (itemName === 'showDetail') {
        this.$emit('showDetailModal', JSON.parse(JSON.stringify(row)))
      } else if (itemName === 'showDistributionAbove') {
        this.$emit('showDistributionAbove', row)
      } else if (itemName === 'showDistributionBelow') {
        this.$emit('showDistributionBelow', row)
      } else if (itemName === 'performanceStat') {
        this.$emit('showPerformanceStat', row)
      } else if (itemName === 'putToTop') {
        this.putToTop(row.userId)
      } else if (itemName === 'putToUser') {
        this.putToUser(row.userId)
      } else if (itemName === 'moduleDetail') {
        this.$emit('showModuleDetail', row.userId)
      } else if (itemName === 'moduleSearch') {
        this.$emit('showModuleSearch')
      }  
    },
    showDetail(row) {},
    select(selections, row) {
              utils.select(this, selections, row)
            },
            selectCancel(selections, row) {
              utils.selectCancel(this, selections, row)
            },
            selectAll(selections) {
              utils.selectAll(this, selections)
            },
            selectAllCancel(selections) {
              utils.selectAllCancel(this, selections)
            },
    changeSort(sortColumn) {
      utils.changeSort(this, sortColumn)
    },
    changePageNo(pageNo) {
      utils.changePageNo(this, pageNo)
    },
    changePageSize(pageSize) {
      utils.changePageSize(this, pageSize)
    },
    putToTop(userId) {
      this.$Modal.confirm({
        title: '确认？',
        content: '确认将此用户设置为顶级分销商吗？',
        onOk: () => {
          utils.doGet('/distribution/admin/to-top/' + userId, {}).then(response => {
            if (response.data.code === ResponseStatus.OK) {
              this.$Message.info(response.data.message)
              this.search()
            } else {
              this.$Message.error(response.data.message)
            }
          }).catch(error => {
            console.log(error)
          })
        },
        onCancel: () => {

        }
      })
    },
    putToUser(userId) {
      this.$Modal.confirm({
          title: '重设上级',
          render: (h) => {
              return h('Input', {
                  props: {
                      value: this.toUserId,
                      autofocus: true,
                      placeholder: '请输入直接上级用户编号',
                  },
                  on: {
                      input: (val) => {
                          this.toUserId = val
                      }
                  }
              })
          },
          onOk: () => {
            utils.doGet('/distribution/admin/to-user/' + userId + '/' + this.toUserId, {}).then(response => {
            if (response.data.code === ResponseStatus.OK) {
              this.$Message.info(response.data.message)
              this.toUserId = null
              this.search()
            } else {
              this.$Message.error(response.data.message)
              this.toUserId = null
            }
          }).catch(error => {
            console.log(error)
          })
          }
      })
    }
  }
}
</script>

<style>
</style>
